package com.atguigu.study.block;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;

public class BlockingQueueDemo {
    public static void main(String[] args) throws InterruptedException {
        ArrayBlockingQueue<String> arrayBlockingQueue = new ArrayBlockingQueue<>(3);
        //报异常组  add  remove
//        System.out.println(arrayBlockingQueue.add("a"));
//        System.out.println(arrayBlockingQueue.add("b"));
//        System.out.println(arrayBlockingQueue.add("c"));
         //        抛出异常IllegalStateException:Queue full
//        System.out.println(arrayBlockingQueue.add("d"));

//        System.out.println(arrayBlockingQueue.element());

//        System.out.println(arrayBlockingQueue.remove());
//        System.out.println(arrayBlockingQueue.remove());
//        System.out.println(arrayBlockingQueue.remove());
        //抛出异常 NoSuchElementException
//        System.out.println(arrayBlockingQueue.remove());

        //不报异常组  offer poll
//        System.out.println(arrayBlockingQueue.offer("a"));
//        System.out.println(arrayBlockingQueue.offer("b"));
//        System.out.println(arrayBlockingQueue.offer("c"));
        // 结果为false 不报异常
//        System.out.println(arrayBlockingQueue.offer("d"));
//        System.out.println(arrayBlockingQueue.poll());
//        System.out.println(arrayBlockingQueue.poll());
//        System.out.println(arrayBlockingQueue.poll());
       // 结果为null
//        System.out.println(arrayBlockingQueue.poll());


         //阻塞 put take
//        arrayBlockingQueue.put("a");
//        arrayBlockingQueue.put("b");
//        arrayBlockingQueue.put("c");
        //到这阻塞
//       arrayBlockingQueue.put("d");
//        System.out.println(arrayBlockingQueue.take());
//        System.out.println(arrayBlockingQueue.take());
//        System.out.println(arrayBlockingQueue.take());
        //到这阻塞
//        System.out.println(arrayBlockingQueue.take());


           //超时 offer poll
        System.out.println(arrayBlockingQueue.offer("a"));
        System.out.println(arrayBlockingQueue.offer("c"));
        System.out.println(arrayBlockingQueue.offer("b"));
          //到这阻塞三秒 时间一道就继续往下执行 false
//        System.out.println(arrayBlockingQueue.offer("d",3, TimeUnit.SECONDS));

        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        System.out.println(arrayBlockingQueue.poll());
        //到这阻塞三秒 时间一道就继续往下执行  null
//        System.out.println(arrayBlockingQueue.poll(3,TimeUnit.SECONDS));
    }
}
